<?php

/**
 * @author gbfun <gbfun@163.com>  2018-06-20 16:28
 * http://www.wuweiyou.com
 */

class db_model_order
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    /************** @by gbfun @start 关键【依赖】方法 尽可能少的调用依赖；能在依赖【内部调用】实现的功能，尽可能不要在【依赖外部】调用实现 **************/
    protected function _get_write_db()
    {
        return wwy_resource_container::get_write_db();
    }
    
    protected function _get_read_db()
    {
        return wwy_resource_container::get_db();
    }
    /************** @by gbfun @end 关键【依赖】方法 **************/
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_order::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function get_formatter()
    {
        if (null === $this->_formatter) {
            $this->_formatter = formatter_order::get_instance();
        }
    
        return $this->_formatter;
    }
    
    public function set_rows(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row($row);            
        }
    }
    
    public function set_row(& $row)
    {
        $formatter = $this->get_formatter();
        
        if(!empty($row['pay_time'])){
            $formatter->set_by_pay_time($row);
        }
        if(!empty($row['shipping_time'])){
            $formatter->set_by_shipping_time($row);
        }
        if(!empty($row['confirm_receive_time'])){
            $formatter->set_by_confirm_receive_time($row);
        }
        if(!empty($row['confirm_return_time'])){
            $formatter->set_by_confirm_return_time($row);
        }
        if(!empty($row['confirm_time'])){
            $formatter->set_by_confirm_time($row);
        }
        
        
        if(!empty($row['create_time'])){
            $formatter->set_by_create_time($row);
        }
        if(!empty($row['update_time'])){
            $formatter->set_by_update_time($row);
        }
        if(isset($row['status'])){
            $formatter->set_by_status($row);
        }
        
        $model_foreground_address_region = wwy_resource_container::get_model_foreground_address_region();
        //var_dump($model_foreground_address_region); exit();
        if(!empty($row['province_id'])){
            $province = $model_foreground_address_region->find($row['province_id']);
            $row['province_name'] = $province['name'];
        }
        if(!empty($row['city_id'])){
            $city = $model_foreground_address_region->find($row['city_id']);
            $row['city_name'] = $city['name'];
        }
        if(!empty($row['district_id'])){
            $district = $model_foreground_address_region->find($row['district_id']);
            $row['district_name'] = $district['name'];
        }
        if(!empty($row['street_id'])){
            $street = $model_foreground_address_region->find($row['street_id']);
            $row['street_name'] = $street['name'];
        }else{
            $row['street_name'] = '';
        }
        
        //备用：下单人的手机号码，而不是收件人的手机号码。收件人的手机号码已有对应【字段：`order_order`.`mobile`】
        $model_foreground_member = wwy_resource_container::get_model_foreground_member();
        if(!empty($row['member_id'])){
            $member = $model_foreground_member->find($row['member_id']);
            $row['member_mobile'] = $member['mobile'];
        }else{
            $row['member_mobile'] = '';
        }
    }
    
    public function set_rows_user(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row_user($row);
        }
    }
    
    public function set_row_user(& $row)
    {
//         $this->get_formatter()->set_by_create_time($row);
//         $this->get_formatter()->set_by_update_time($row);
    }
    
    public function find($id)
    {
        $row = $this->get_db_table()->find($id);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_row($where)
    {
        $row = $this->get_db_table()->fetch_row($where);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_all($where = null, $order = null)
    {
        $rows = $this->get_db_table()->fetch_all($where, $order);
        //var_dump($rows); exit();
    
        $this->set_rows($rows);
        //var_dump($rows); exit();
    
        return $rows;
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $list = $this->get_db_table()->get_list($where, $order, $options);
        //var_dump($list); exit();
    
//         $rows = $list['list'];
//         $this->set_rows($rows);
//         $list['list'] = $rows;
        $this->set_rows($list['list']);
        //var_dump($list); exit();
    
        return $list;
    }
    
    public function get_simple_list($where = null, $order = null, $options = array())
    {
    
    }  
    
    public function add_order($order_info = array())
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
        
        $cart_list_info = $order_info['cart_list_info'];
        $member_info    = $order_info['member_info'];
        $address_info   = $order_info['shipping_info']['address_info'];      
    
        $order_sn = $this->generate_order_sn();
        $status = 0;
        $time = time();
        $order_options = array(
            'order_sn'          => $order_sn,
            'member_id'         => $member_info['member_id'],
            'receiver'          => $address_info['receiver'],
            'province_id'       => $address_info['province_id'],
            'city_id'           => $address_info['city_id'],
            'district_id'       => $address_info['district_id'],
            'street_id'         => $address_info['street_id'],
            'address'           => $address_info['address'],
            'tel'               => $address_info['tel'],
            'mobile'            => $address_info['mobile'],
            'good_total_price'  => $cart_list_info['total'],
            'order_total_price' => $cart_list_info['total'],
            'create_time'       => $time,
            'update_time'       => $time,
            'status'            => $status, 
        );
        //print_r($order_options); exit();
    
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
	    $pdo = $write_db->get_connection();
	    //var_dump($pdo); exit();
	    $pdo->beginTransaction();
        try{
            $order_id = $this->get_db_table()->add($order_options);
            //var_dump($order_id); exit();
            if($order_id){
                $order_info['order_id'] = $order_id;
                $model_foreground_order_good = wwy_resource_container::get_model_foreground_order_good();
                //var_dump($model_foreground_order_good); exit();
                $order_good_id_list = $model_foreground_order_good->add_order_good($order_info);    //@by gbfun 
                //var_dump($order_good_id_list); exit();
                
                $model_foreground_cart = wwy_resource_container::get_model_foreground_cart();
                //var_dump($model_foreground_cart); exit();
                $clear_cart_result = $model_foreground_cart->clear_cart($member_info['member_id']);
                //var_dump($clear_cart_result); exit();
    
                $result['data']['order_id'] = $order_id;
            }
            	
            $pdo->commit();
        }catch (Exception $e){
            $pdo->rollBack();
            $result['status']  = -1;
        }
    
        return $result;
    }
    
    public function generate_order_sn()
    {
        $server_prefix = '01';
        $date          = date('Ymd');
        $prefix        = $server_prefix . '.' . $date . '.';
        $order_sn      = uniqid($prefix, TRUE);
        $order_sn      = str_replace('.', '', $order_sn);
    
        return $order_sn;
    }
    
    public function update_order_status($order_info)
    {
        $update_order_status_result = $this->get_db_table()->update_order_status($order_info);
        //var_dump($update_order_status_result);
    
        return $update_order_status_result;
    }
    
    public function set_paid($id)
    {
        $time = time();
        $order_info = array(
            'id'          => $id,
            'status'      => 1,
            'pay_time'    => $time,
            'update_time' => $time
        );
         
        $set_paid_result = $this->get_db_table()->set_paid($order_info);
        //var_dump($set_paid_result); exit(); 
    
        return $set_paid_result;
    }
    
    public function set_confirm_received($order_info)
    {         
        $set_confirm_received_result = $this->get_db_table()->set_confirm_received($order_info);
        //var_dump($set_confirm_received_result); exit();
    
        return $set_confirm_received_result;
    }
    
    public function set_confirm_return($order_info)
    {
        $set_confirm_return_result = $this->get_db_table()->set_confirm_return($order_info);
        //var_dump($set_confirm_return_result); exit();
    
        return $set_confirm_return_result;
    }
}

